package com.springbootee.Controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.springbootee.Common.Rest;
import com.springbootee.Common.base.UserHolder;
import com.springbootee.Service.UserService;
import com.springbootee.entity.User;
import com.springbootee.uitl.BasePageResult;
import com.springbootee.uitl.UserInfoPage;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import jdk.nashorn.internal.parser.Token;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user")
@Api(value="用户模块",tags = "用户模块")
public class UserController {
    private final UserService userService;
    public UserController(UserService userService){
        this.userService=userService;
    }
//
//    @Api：修饰整个类，描述Controller的作用
//
//    @ApiOperation：描述一个类的一个方法，或者说一个接口
//
//    @ApiParam：单个参数描述
//
//    @ApiModel：用对象来接收参数
//
//    @ApiProperty：用对象接收参数时，描述对象的一个字段
    @PostMapping("/login")
    @ApiOperation("用户登录")
    public Rest<?> UserLogin(@RequestBody User user){
        user=userService.login(user);
        return   Rest.OK("登录成功",user);
    }

    @PostMapping("/register")
    @ApiOperation("用户注册")
    public  Rest<?> UserRegister(@RequestBody User user){
        user=userService.register(user);
        return  Rest.OK("注册成功",user);
    }

    @GetMapping("/mine")
    @ApiOperation("获取当前用户信息")
    public  Rest<?> GetMyInfo(Token token){
        return  Rest.OK(UserHolder.getUser());
    }

    @GetMapping("")
    @ApiOperation("获取用户列表")
    public  Rest<?> GetList(){
    return  Rest.OK(userService.list(new QueryWrapper<>()));
    }


    @GetMapping("/getUser/{id}")
    @ApiOperation("获取单个用户信息")
    public Rest<?> GetDetail(@PathVariable("id") Long id){
        return  Rest.OK(userService.getById(id));
    }

    @PostMapping("/deleteUser/{id}")
    @ApiOperation("删除单个用户")
    public  Rest<?> DeleDetail(@PathVariable("id") Long id){
        return  Rest.OK(userService.removeById(id));
    }

    @PostMapping("/updateUser/{id}")
    @ApiOperation("编辑单个用户")
    public Rest<?> GetUserId(@PathVariable("id") User user ){
        return  Rest.OK(userService.updateById(user));
    }

    @PostMapping("/page")
    @ApiOperation("分页查询用户")
    public  Rest<BasePageResult<User>> page(@RequestBody UserInfoPage userPage){
            IPage<User> pageParam=new Page<>(userPage.getPageNum(),userPage.getPageSize());
            IPage<User> pageResult=userService.page(pageParam,new LambdaQueryWrapper<User>()
                    .like(User::getUserName,userPage.getKeyword())
            );
            return  Rest.OK(BasePageResult.newInstance(pageResult));

    }

}
